﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using ABCat.Core;
using ABCat.Core.Plugins;
using ABCat.Core.Plugins.DataSets;
using ABCat.Core.Plugins.UI;
using ABCat.Shared.Properties;

namespace ABCat.Plugins.NormalizationSettingsEditor.Standard
{
	/// <summary>
	/// Interaction logic for NormalizationSettingsUc.xaml
	/// </summary>
	[PluginCreator("Normalization Settings Viewer Standard", WorkModeEnum.PerCall),
	UsedImplicitly]
	public sealed class NormalizationSettingsViewModel : INormalizationSettingsEditorPlugin, INotifyPropertyChanged
	{
		#region + Events +

		public event EventHandler Disposed;
		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
		#region + Fields +

		private readonly NormalizationEditorViewModel _normalizationEditorViewModel;
		private readonly NormalizationViewerViewModel _normalizationViewerViewModel;

		private NormalizationSettingsUc _control;
		private bool _editorVisible;
		private bool _isOnUpdate;
		private bool _viewerVisible;

		#endregion
		#region + Properties +

		public Config Config		{get; set;}
		public FrameworkElement Control
		{
			get
			{
				if (_control == null)
				{
					_control = new NormalizationSettingsUc(this);
					ViewerVisible = true;
				}
				return _control;
			}
		}

		public bool EditorVisible
		{
			get { return _editorVisible; }
			set
			{
				if (value.Equals(_editorVisible)) return;
				_editorVisible = value;
				OnPropertyChanged();
				if (value) ViewerVisible = false;
			}
		}

		public bool IsOnUpdate
		{
			get { return _isOnUpdate; }
			private set
			{
				if (_isOnUpdate == value) return;
				_isOnUpdate = value;
				OnPropertyChanged();
			}
		}

		public NormalizationEditorViewModel NormalizationEditorViewModel
		{
			get { return _normalizationEditorViewModel; }
		}

		public NormalizationViewerViewModel NormalizationViewerViewModel
		{
			get { return _normalizationViewerViewModel; }
		}

		public IEnumerable<IAudioBook> TargetRecordsForEdit
		{
			get { return _normalizationEditorViewModel.TargetRecords; }
			set
			{
				_normalizationEditorViewModel.TargetRecords = value;
				EditorVisible = value.AnySafe();
				ViewerVisible = !value.AnySafe();
			}
		}

		public bool ViewerVisible
		{
			get { return _viewerVisible; }
			set
			{
				if (value.Equals(_viewerVisible)) return;
				_viewerVisible = value;
				OnPropertyChanged();
				if (value)
				{
					_normalizationViewerViewModel.BeginUpdateReplacementTreeSourceAsync();
					EditorVisible = false;
				}
			}
		}

		#endregion
		#region + Ctor +

		public NormalizationSettingsViewModel( Context context )
		{
			_normalizationViewerViewModel = new NormalizationViewerViewModel(context);
			_normalizationEditorViewModel = new NormalizationEditorViewModel(context);
			_normalizationEditorViewModel.PropertyChanged += _normalizationEditorViewModel_PropertyChanged;
		}

		#endregion
		#region + Logic +

		public void BeginRefreshDataAsync()
		{
			_normalizationViewerViewModel.BeginUpdateReplacementTreeSourceAsync();
		}

		public bool CheckForConfig(bool correct, out Config incorrectConfig)
		{
			incorrectConfig = null;
			return true;
		}

		public void Dispose()
		{
			var control = _control;
			if (control != null)
			{
				_control = null;
			}
			Disposed.Fire(this, EventArgs.Empty);
		}

		public void RestoreLayout()
		{
		}

		public void StoreLayout()
		{
		}

		[NotifyPropertyChangedInvocator]
		private void OnPropertyChanged([CallerMemberName] string propertyName = null)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
		}

		private void _normalizationEditorViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "IsOnEditMode":
					ViewerVisible = !_normalizationEditorViewModel.IsOnEditMode;
					EditorVisible = _normalizationEditorViewModel.IsOnEditMode;
					break;
			}
		}

		#endregion
	}
}